package com.jic.market.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.utils.IDUtils;
import com.jic.market.common.constant.CouponConstant;
import com.jic.market.entity.CouponConfigTemplate;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.mapper.CouponConfigTemplateMapper;
import com.jic.market.request.manager.coupon.template.CouponTemplateAddRequest;
import com.jic.market.request.manager.coupon.template.CouponTemplateDeleteRequest;
import com.jic.market.request.manager.coupon.template.CouponTemplateEditRequest;
import com.jic.market.request.manager.coupon.template.CouponTemplateListQueryRequest;
import com.jic.market.request.manager.coupon.template.TemplateRequest;
import com.jic.market.response.CouponConfigQueryResponse;
import com.jic.market.response.manager.coupon.template.CouponTemplateListQueryResponse;
import com.jic.market.service.CouponConfigTemplateService;

import org.apache.commons.collections.CollectionUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券配置模版表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-03 10:52:21
 */
@Service
public class CouponConfigTemplateServiceImpl implements CouponConfigTemplateService {

    @Autowired
    private CouponConfigTemplateMapper couponConfigTemplateMapper;

    @Override
    public long insert(CouponConfigTemplate couponConfigTemplate) {
        long flag = couponConfigTemplateMapper.insert(couponConfigTemplate);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return couponConfigTemplateMapper.delete(id);
    }

    @Override
    public long update(CouponConfigTemplate couponConfigTemplate) {
        return couponConfigTemplateMapper.update(couponConfigTemplate);
    }

    @Override
    public CouponConfigTemplate queryByPrimaryKey(Long id) {
        return couponConfigTemplateMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<CouponConfigTemplate> query(CouponConfigTemplate couponConfigTemplate) {
        return couponConfigTemplateMapper.query(couponConfigTemplate);
    }

    @Override
    public PageResult<CouponConfigTemplate> queryPage(CouponConfigTemplate couponConfigTemplate, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<CouponConfigTemplate> list = couponConfigTemplateMapper.queryPage(startIndex, pageQuery.getLimit(), couponConfigTemplate);
        long count = couponConfigTemplateMapper.queryPageCount(couponConfigTemplate);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<String> addCouponTemplate(CouponTemplateAddRequest couponTemplateAddRequest) {
        CouponConfigTemplate template=new CouponConfigTemplate();
        BeanUtils.copyProperties(couponTemplateAddRequest,template);
        template.setTemplateId(IDUtils.GenerateBillNo("tem"));
        template.setCouponStatus(PromotionStatusEnum.DRAFT.getCode().toString());
        template.setDeleteFlag(false);
        insert(template);
        return RestResult.success(template.getTemplateId());
    }

    @Override
    public RestResult<CouponConfigQueryResponse> queryCouponTemplate(TemplateRequest templateRequest) {
        CouponConfigTemplate template=couponConfigTemplateMapper.queryCouponConfigTemplateByTemplateId(templateRequest.getTemplateId());
        if(template==null){
            return RestResult.error("9999","该模版Id无效");
        }
        CouponConfigQueryResponse response=new CouponConfigQueryResponse();
        BeanUtils.copyProperties(template,response);
        return RestResult.success(response);
    }

    @Override
    public RestResult<Boolean> deleteCouponTemplate(CouponTemplateDeleteRequest couponTemplateDeleteRequest) {
        long flag=couponConfigTemplateMapper.deleteCouponConfigTemplateByTemplateId(couponTemplateDeleteRequest.getTemplateId());
        if(flag>0){
            return RestResult.success(true);
        }
        return RestResult.success(false);
    }

    @Override
    public RestResult<Boolean> editCouponTemplate(CouponTemplateEditRequest couponTemplateEditRequest) {
        CouponConfigTemplate template=new CouponConfigTemplate();
        BeanUtils.copyProperties(couponTemplateEditRequest,template);
        long flag=couponConfigTemplateMapper.update(template);
        if(flag>0){
            return RestResult.success(true);
        }
        return RestResult.success(false);
    }

    @Override
    public RestResult<PageResult<CouponTemplateListQueryResponse>> queryCouponTemplateList(CouponTemplateListQueryRequest couponTemplateListQueryRequest) {
        Page page=new Page();
        page.setStart(couponTemplateListQueryRequest.getStart());
        page.setLimit(couponTemplateListQueryRequest.getLimit());

        CouponConfigTemplate template=new CouponConfigTemplate();
        PageResult<CouponConfigTemplate> pageResult=queryPage(template,page);

        PageResult<CouponTemplateListQueryResponse> responsePageResult=new PageResult<>();
        responsePageResult.setTotal(pageResult.getTotal());

        if(CollectionUtils.isEmpty(pageResult.getRows())){
            responsePageResult.setRows(new ArrayList<>());
            return RestResult.success(responsePageResult);
        }
        List<CouponTemplateListQueryResponse> responseList=new ArrayList<>();
        pageResult.getRows().stream().forEach(item->{
            CouponTemplateListQueryResponse response=new CouponTemplateListQueryResponse();
            response.setId(item.getTemplateId());
            response.setCreateId(getValue(item.getCreator()));
            response.setCreateName(getValue(item.getCreator()));
            response.setCreateTime(item.getCreateTime());
            response.setTemplateId(item.getTemplateId());
            response.setTemplateName(item.getTemplateName());
            responseList.add(response);
        });

        responsePageResult.setRows(responseList);
        return RestResult.success(responsePageResult);
    }

    public String getValue(Long param){
        if(param==null){
            return "";
        }
        return Long.toString(param);
    }
}